package com.syq.controller;

import com.syq.constant.CacheConstants;
import com.syq.enums.InterfaceExceptionEnums;
import com.syq.exception.SmsException;
import com.syq.feign.CacheFeign;
import com.syq.form.SmsInterfaceForm;

import com.syq.service.impl.InterfaceFormServiceImpl;
import com.syq.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName: InterfaceController
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/8/24 15:23
 * 接受客戶下行数据*
 */

@RestController
@RequestMapping("/sms")
public class InterfaceController {

    @Value("${ip-header}")
    private String[] ipHeaders;

    @Autowired
    private CacheFeign cacheFeign;

    @Autowired
    private InterfaceFormServiceImpl interfaceFormService;

    @PostMapping("/smsInterface")
    public Object interfaceForm(SmsInterfaceForm smsInterfaceForm, HttpServletRequest request){
        String key= CacheConstants.CACHE_PREFIX_CLIENT+smsInterfaceForm.getClientID();
        Map user = cacheFeign.hMGet(key);
        //1.判断当前用户是否存在
        if (null==user||user.size()<=0){
            return Result.error(new SmsException(InterfaceExceptionEnums.INTERFACE_CLIENT_ID_ERROR));
        }
        //判断当前用户密码
        String password = smsInterfaceForm.getPwd();
        String pwd = (String) user.get("pwd");
        System.out.println(password+"|"+pwd);
        if(StringUtils.isEmpty(password) || !password.equals(pwd)){
            return Result.error(new SmsException(InterfaceExceptionEnums.INTERFACE_PWD_ERROR));
        }

        //短信内容的长度
        String content = smsInterfaceForm.getContent();
        if (StringUtils.isEmpty(content)||content.length()>500){
            return Result.error(new SmsException(InterfaceExceptionEnums.INTERFACE_CONTENT_ERROR));
        }
        //编号验证
        String srcId=smsInterfaceForm.getSrcID();
        if (StringUtils.isEmpty(srcId)){
            return Result.error(new SmsException(InterfaceExceptionEnums.INTERFACE_SRC_ID_ERROR));
        }
        // ip验证
        String ip=getIp(request);
        smsInterfaceForm.setIp(ip);
        boolean flag=false;
        String ipaddress = (String) user.get("ipaddress");
        if (!StringUtils.isEmpty(ipaddress)){
            String[] ips = ipaddress.split(",");
            for (String p : ips) {
                if (p.equals(ip)){
                    flag=true;
                }
            }
        }
        if (!flag){
            return Result.error(new SmsException(InterfaceExceptionEnums.INTERFACE_IP_ERROR));
        }
        //手机号验证
        String mobile = smsInterfaceForm.getMobile();
        if (StringUtils.isEmpty(mobile)){
            return Result.error(105,"手机号不能为空");
        }
        String[] mobiles = mobile.split(",");
        String regex = "^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
        if (StringUtils.isEmpty(mobiles)||mobiles.length>100||mobiles.length<=0){
            return Result.error(new SmsException(InterfaceExceptionEnums.INTERFACE_MOBILE_ERROR));
        }
        Set<String> erroMobile=new HashSet<>();
        for (String s : mobiles) {
            if(!s.matches(regex)) {
                erroMobile.add(s);
            }
        }
        if (erroMobile.size()>0){
            return Result.error(new SmsException(InterfaceExceptionEnums.INTERFACE_MOBILE_ERROR));

        }

        interfaceFormService.submitInterfaceForm(smsInterfaceForm);
        return Result.ok();
    }

    public String getIp(HttpServletRequest request){
        for (String ipHeader : ipHeaders) {
            String ip=request.getHeader(ipHeader);
            if (!StringUtils.isEmpty(ip)||"unknown".equalsIgnoreCase(ip)){
               return ip;
            }
        }
        return request.getRemoteAddr();
    }
}